<template>
	<view>
		<!--搜索框-->
		<view class="search_top">
			<my-search @click="searchaAction"></my-search>
		</view>
		<!--轮播图数据-->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item class="swiper-item" v-for="(item, i) in swiperData" :key = "i">
				<navigator :url="'/subpkg/goods_detail/goods_detail?id=' + item.goods_id">
					<image :src="item.image_src" mode="widthFix"></image>
				</navigator>
			</swiper-item>
		</swiper>
		<!--导航数据-->
		<view class="nav_list">
			<view class="nav_item" v-for="(item, i) in navListData" :key = "i" @click="itemClick(item)">
				<image :src="item.image_src" class="nav_img"></image>
			</view>
		</view>
		
		<!--楼层数据-->
		<view class="floor_list">
			<view class="floor_list_item" v-for="(floorModel, i) in floorData" :key = "i">
				<image :src="floorModel.floor_title.image_src" class="floor_title"></image>
				
				<view class="floor_box">
					<navigator class="left_img_box" :url="floorModel.product_list[0].url">
						<image :src="floorModel.product_list[0].image_src" :style="{width: floorModel.product_list[0].image_width +'rpx'}" mode="widthFix"></image>
					</navigator>
					<view class="right_img_box">
						<navigator class="right_img_item" v-for="(item, index) in floorModel.product_list" :key="index" v-if="index !== 0" :url="item.url">
							<image :src="item.image_src" :style="{width: item.image_width + 'rpx'}" mode="widthFix"></image>
						</navigator>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		mixins: [badgeMix],
		
		data() {
			return {
				swiperData: [],
				navListData: [],
				floorData: []
			}
		},
		
		onLoad() {
			this.getSwiperData()
			this.getNavListData()
			this.getFloorData()
		},
		
		methods: {
			async getSwiperData() {
				//res 对应为接口返回的data层
				const {data: res} = await uni.$http.get('/api/public/v1/home/swiperdata')
	
				if (res.meta.status !== 200) return uni.$showMsg()
				
				this.swiperData = res.message
			},
			
			async getNavListData() {
				const {data: res} = await uni.$http.get('/api/public/v1/home/catitems')
				
				if (res.meta.status !== 200) return uni.$showMsg()
				
				this.navListData = res.message
			},
			
			async getFloorData() {
				const {data: res} = await uni.$http.get('/api/public/v1/home/floordata')
				
				if (res.meta.status !== 200) return uni.$showMsg()
				
				//跟model添加自定义数据
				res.message.forEach(floor => {
					floor.product_list.forEach(product => {
						product.url = '/subpkg/goods_list/goods_list?' + product.navigator_url.split('?')[1]
					})
				})
				
				this.floorData = res.message
			},
			
			itemClick(item) {
				if (item.name === '分类') {
					uni.switchTab({
						url: '../category/category'
					})
				}
			},
			
			searchaAction() {
				uni.navigateTo({
					url:'../../subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
	//吸顶效果
	.search_top {
		position: sticky; //粘,ps: class必须是view盒子才会有效果
		top: 0;
		z-index: 999;
	}
	swiper {
		height: 330rpx;
		
		.swiper-item,
		image {
			height: 100%;
			width: 100%;
		}
	}
	
	.nav_list {
		margin: 15px 0;
		display: flex;
		justify-content: space-around;
		
		.nav_img {
			width: 128rpx;
			height: 140rpx;
		}
	}
	
	.floor_title {
		height: 60rpx;
		width: 100%;
	}
	
	.floor_box {
		display: flex;
		padding-left: 10rpx;
	}
		
	.right_img_box {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-around;
	}
	
</style>
